package com.ha7419x.rest;

import com.ha7419x.service.IDeptService;
import com.ha7419x.vo.Dept;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
public class DeptRest {

    @Resource
    private IDeptService deptService;


    @RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
    @HystrixCommand(fallbackMethod = "getFallBack") //如果当前调用的 get 方法出错了 则调用 fallback
    public Object get(@PathVariable("id") Long id){
        Dept vo = this.deptService.get(id);
        if(vo == null){
            throw new RuntimeException("部门不存在");
        }
        return this.deptService.get(id);
    }

    public Object getFallBack(@PathVariable("id") Long id){
        Dept dept = new Dept();
        dept.setDeptNo("9999");
        dept.setDeptName("【ERROR】demo-provider-dept-hystrix-8001");
        dept.setLoc("【ERROR】demo-provider-dept-hystrix-8001");
        return dept;
    }

}
